<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
	<meta name="keywords" content="Java,SDK" />
	<meta name="description" content="Java SDK" />
	<!-- 网页标签标题 -->
	<title>Java SDK</title>
	<link rel="shortcut icon" href="https://img.alicdn.com/tfs/TB1hgJpHAPoK1RjSZKbXXX1IXXa-64-64.png"/>
	<link rel="stylesheet" href="/build/documentation.css" />
</head>
<body>
	<div id="root"><div class="documentation-page" data-reactroot=""><header class="header-container header-container-normal"><div class="header-body"><a href="/en-us/index.html"><img class="logo" src="/img/nacos_colorful.png"/></a><div class="search search-normal"><span class="icon-search"></span></div><span class="language-switch language-switch-normal">中</span><div class="header-menu"><img class="header-menu-toggle" src="/img/menu_gray.png"/><ul><li class="menu-item menu-item-normal"><a href="/en-us/index.html">HOME</a></li><li class="menu-item menu-item-normal menu-item-normal-active"><a href="/en-us/docs/quick-start.html">DOCS</a></li><li class="menu-item menu-item-normal"><a href="/en-us/blog">BLOG</a></li><li class="menu-item menu-item-normal"><a href="/en-us/community">COMMUNITY</a></li><li class="menu-item menu-item-normal"><a href="https://cn.aliyun.com/product/aliware/mse?spm=nacos-website.topbar.0.0.0">NACOS IN CLOUD</a><img class="menu-img" src="https://img.alicdn.com/tfs/TB1esl_m.T1gK0jSZFrXXcNCXXa-200-200.png"/></li><li class="menu-item menu-item-normal"><a href="http://console.nacos.io/nacos/index.html">DEMO-CONSOLE</a></li></ul></div></div></header><div class="bar"><div class="bar-body"><img src="/img/docs.png" class="front-img"/><div class="bar-title"><span>Documentation</span><div class="bone bone-light"></div></div><img src="/img/docs.png" class="back-img"/></div></div><section class="content-section"><div class="content-body"><div class="sidemenu"><div class="sidemenu-toggle"><img src="https://img.alicdn.com/tfs/TB1E6apXHGYBuNjy0FoXXciBFXa-200-200.png"/></div><ul><li class="menu-item menu-item-level-1"><span>Nacos </span><ul><li style="height:180px;overflow:hidden" class="menu-item menu-item-level-2"><span>What is Nacos<img style="transform:rotate(0deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/what-is-nacos.html" target="_self">What is Nacos</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/concepts.html" target="_self">Concepts</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/architecture.html" target="_self">Architecure</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/feature-list.html" target="_self">Feature list</a></li></ul></li><li style="height:324px;overflow:hidden" class="menu-item menu-item-level-2"><span>Quick Start<img style="transform:rotate(0deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start.html" target="_self">Nacos</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start-spring.html" target="_self">Nacos Spring</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start-spring-boot.html" target="_self">Nacos Spring Boot</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start-spring-cloud.html" target="_self">Nacos Spring Cloud</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start-docker.html" target="_self">Nacos Docker</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/use-nacos-with-dubbo.html" target="_self">Nacos with Dubbo</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/use-nacos-with-kubernetes.html" target="_self">Nacos with K8s</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-sync.html" target="_self">Nacos Sync</a></li></ul></li><li style="height:252px;overflow:hidden" class="menu-item menu-item-level-2"><span>User Guide<img style="transform:rotate(0deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/sdk.html" target="_self">Java SDK</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/other-language.html" target="_self">Other Language</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/open-api.html" target="_self">Open-API</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-spring.html" target="_self">Nacos Spring</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/system-configurations.html" target="_self">Nacos System Configurations</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/faq.html" target="_self">FAQ</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>Admin Guide<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/deployment.html" target="_self">Deployment Guide</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/cluster-mode-quick-start.html" target="_self">Cluster Mode Deployment</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/management-api.html" target="_self">Management OpenAPI</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/console-guide.html" target="_self">Admin Console Guide</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/monitor-guide.html" target="_self">Nacos Monitor Guide</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-config-benchmark.html" target="_self">Nacos Config Benchmark</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-naming-benchmark.html" target="_self">Nacos Naming Benchmark</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-sync-use.html" target="_self">Data Migration to Nacos</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>Contributor Guide<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/contributing.html" target="_self">Contribute to Nacos</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/contributing-flow.html" target="_self">Contributing Flow</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/pull-request.html" target="_self">Pull Request Template</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/how-to-reporting-bugs.html" target="_self">How to report bugs</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>Community<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/community.html" target="_self">Community</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-dev.html" target="_self">Develop Team</a></li></ul></li></ul></li></ul></div><div class="doc-content markdown-body"><h1>Java SDK</h1>
<h2>Overview</h2>
<p>Maven coordinates</p>
<pre><code>&lt;dependency&gt;
&lt;groupId&gt;com.alibaba.nacos&lt;/groupId&gt;
&lt;artifactId&gt;nacos-client&lt;/artifactId&gt;
&lt;version&gt;${version}&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>
<h2>Configuration Management</h2>
<h3>Get configuration</h3>
<h4>Description</h4>
<p>Get configuration from Nacos when a service starts.</p>
<pre><code class="language-java"><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getConfig</span><span class="hljs-params">(String dataId, String group, <span class="hljs-keyword">long</span> timeoutMs)</span> <span class="hljs-keyword">throws</span> NacosException
</span></code></pre>
<h4>Request parameters</h4>
<div class="bi-table">
  <table>
    <colgroup>
      <col width="auto" />
      <col width="auto" />
      <col width="auto" />
    </colgroup>
    <tbody>
      <tr>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Name</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Type</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Description</div>
        </td>
      </tr>
      <tr>
        <td rowspan="1" colSpan="1">
          <div data-type="p">dataId</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">string</div>
          <div data-type="p"></div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Configuration ID. Use a naming rule similar to package.class (for example, com.taobao.tc.refund.log.level) to ensure global uniqueness. It is recommended to indicate business meaning of the configuration in the &quot;class&quot; section. Use
            lower case for all characters. Use alphabetical letters and these four special characters (&quot;.&quot;, &quot;:&quot;, &quot;-&quot;, &quot;_&quot;) only. Up to 256 characters are allowed.</div>
        </td>
      </tr>
      <tr>
        <td rowspan="1" colSpan="1">
          <div data-type="p">group</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">string</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Configuration group. To ensure uniqueness, format such as <strong>product name: module name </strong>(for example, Nacos:Test) is preferred. Use alphabetical letters and these four special characters (&quot;.&quot;, &quot;:&quot;, &quot;-&quot;,
            &quot;_&quot;) only. Up to 128 characters are allowed.</div>
        </td>
      </tr>
      <tr>
        <td rowspan="1" colSpan="1">
          <div data-type="p">timeout</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">long</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Length of configuration read time-out (in ms). Recommended value: 3000.</div>
        </td>
      </tr>
    </tbody>
  </table>
</div>
<h4>Return values</h4>
<table>
<thead>
<tr>
<th style="text-align:left">Type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">string</td>
<td style="text-align:left">configuration value</td>
</tr>
</tbody>
</table>
<h4>Request example</h4>
<pre><code class="language-java"><span class="hljs-keyword">try</span> {
    <span class="hljs-comment">// Initialize the configuration service, and the console automatically obtains the following parameters through the sample code.</span>
	String serverAddr = <span class="hljs-string">"{serverAddr}"</span>;
	String dataId = <span class="hljs-string">"{dataId}"</span>;
	String group = <span class="hljs-string">"{group}"</span>;
	Properties properties = <span class="hljs-keyword">new</span> Properties();
	properties.put(<span class="hljs-string">"serverAddr"</span>, serverAddr);
	ConfigService configService = NacosFactory.createConfigService(properties);
    <span class="hljs-comment">// Actively get the configuration.</span>
	String content = configService.getConfig(dataId, group, <span class="hljs-number">5000</span>);
	System.out.println(content);
} <span class="hljs-keyword">catch</span> (NacosException e) {
    <span class="hljs-comment">// TODO Auto-generated catch block</span>
    e.printStackTrace();
}
</code></pre>
<h4>Exception specification</h4>
<p>A ConfigException is thrown in case of a configuration read time-out or a network error.</p>
<h3>Listen configuration</h3>
<h4>Description</h4>
<p>Use dynamic configuration listening API to enable Nacos to send configuration change notifications.</p>
<pre><code class="language-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">addListener</span><span class="hljs-params">(String dataId, ConfigChangeListenerAdapter listener)</span>
</span></code></pre>
<h4>Request parameters</h4>
<div class="bi-table">
  <table>
    <colgroup>
      <col width="auto" />
      <col width="auto" />
      <col width="auto" />
    </colgroup>
    <tbody>
      <tr>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Name</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Type</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Description</div>
        </td>
      </tr>
      <tr>
        <td rowspan="1" colSpan="1">
          <div data-type="p">dataId</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">string</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Configuration ID. Use a naming rule similar to package.class (for example, com.taobao.tc.refund.log.level) to ensure global uniqueness. It is recommended to indicate business meaning of the configuration in the &quot;class&quot; section. Use
            lower case for all characters. Use alphabetical letters and these four special characters (&quot;.&quot;, &quot;:&quot;, &quot;-&quot;, &quot;_&quot;) only. Up to 256 characters are allowed.</div>
        </td>
      </tr>
      <tr>
        <td rowspan="1" colSpan="1">
          <div data-type="p">group</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">string</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Configuration group. To ensure uniqueness, format such as <strong>product name: module name </strong> (for example, Nacos:Test) is preferred. Use alphabetical letters and these four special characters (&quot;.&quot;, &quot;:&quot;, &quot;-&quot;,
            &quot;_&quot;) only. Up to 128 characters are allowed.</div>
        </td>
      </tr>
      <tr>
        <td rowspan="1" colSpan="1">
          <div data-type="p">listener</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Config Change Listener</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Listener. Configuration changes go into the callback function of the listener.</div>
        </td>
      </tr>
    </tbody>
  </table>
</div>
<h4>Return values</h4>
<table>
<thead>
<tr>
<th style="text-align:left">Type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">string</td>
<td style="text-align:left">Configuration value. This value is returned through the callback function during initialization or configuration modification.</td>
</tr>
</tbody>
</table>
<h4>Request example</h4>
<pre><code class="language-java">
<span class="hljs-comment">// Initialize the configuration service, and the console automatically obtains the following parameters through the sample code.</span>
String serverAddr = <span class="hljs-string">"{serverAddr}"</span>;
String dataId = <span class="hljs-string">"{dataId}"</span>;
String group = <span class="hljs-string">"{group}"</span>;
Properties properties = <span class="hljs-keyword">new</span> Properties();
properties.put(<span class="hljs-string">"serverAddr"</span>, serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
String content = configService.getConfig(dataId, group, <span class="hljs-number">5000</span>);
System.out.println(content);
configService.addListener(dataId, group, <span class="hljs-keyword">new</span> Listener() {
	<span class="hljs-meta">@Override</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">receiveConfigInfo</span><span class="hljs-params">(String configInfo)</span> </span>{
		System.out.println(<span class="hljs-string">"receive1:"</span> + configInfo);
	}
	<span class="hljs-meta">@Override</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> Executor <span class="hljs-title">getExecutor</span><span class="hljs-params">()</span> </span>{
		<span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
	}
});

<span class="hljs-comment">// Keep the main thread alive throughout the test, because the configuration subscription runs in a daemon thread, which exits once the main thread exits. The following code is not required in a real environment.</span>
<span class="hljs-keyword">while</span> (<span class="hljs-keyword">true</span>) {
    <span class="hljs-keyword">try</span> {
        Thread.sleep(<span class="hljs-number">1000</span>);
    } <span class="hljs-keyword">catch</span> (InterruptedException e) {
        e.printStackTrace();
    }
}
</code></pre>
<h3>Delete Listening</h3>
<h4>Description</h4>
<p>Cancel listen configuration. No more notification after cancellation.</p>
<pre><code class="language-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">removeListener</span><span class="hljs-params">(String dataId, String group, Listener listener)</span>
</span></code></pre>
<h4>Request parameters</h4>
<div class="bi-table">
  <table>
    <colgroup>
      <col width="auto" />
      <col width="auto" />
      <col width="auto" />
    </colgroup>
    <tbody>
      <tr>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Name</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Type</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Description</div>
        </td>
      </tr>
      <tr>
        <td rowspan="1" colSpan="1">
          <div data-type="p">dataId</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">string</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Configuration ID. Use a naming rule similar to package.class (for example, com.taobao.tc.refund.log.level) to ensure global uniqueness. It is recommended to indicate business meaning of the configuration in the &quot;class&quot; section. Use
            lower case for all characters. Use alphabetical letters and these four special characters (&quot;.&quot;, &quot;:&quot;, &quot;-&quot;, &quot;_&quot;) only. Up to 256 characters are allowed.</div>
        </td>
      </tr>
            <tr>
              <td rowspan="1" colSpan="1">
                <div data-type="p">group</div>
              </td>
              <td rowspan="1" colSpan="1">
                <div data-type="p">string</div>
              </td>
              <td rowspan="1" colSpan="1">
                <div data-type="p">Configuration group
                </td>
            </tr>
      <tr>
        <td rowspan="1" colSpan="1">
          <div data-type="p">listener</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">ConfigChangeListenerAdapter</div>
        </td>
        <td rowspan="1" colSpan="1">
          <div data-type="p">Listener. Configuration changes go into the callback function of the listener.</div>
        </td>
      </tr>
    </tbody>
  </table>
</div>
<h4>Request example</h4>
<pre><code class="language-java">String serverAddr = <span class="hljs-string">"{serverAddr}"</span>;
String dataId = <span class="hljs-string">"{dataId}"</span>;
String group = <span class="hljs-string">"{group}"</span>;
Properties properties = <span class="hljs-keyword">new</span> Properties();
properties.put(<span class="hljs-string">"serverAddr"</span>, serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
configService.removeListener(dataId, group, yourListener);
</code></pre>
<h3>Publish configuration</h3>
<h4>Description</h4>
<p>Publish Nacos configurations automatically to reduce the operation and maintenance cost.</p>
<p><strong>Note:</strong> It uses the same publishing interface to create or modify a configuration. If the specified configuration doesn’t exist, it will create a configuration. If the specified configuration exists, it will update the configuration.</p>
<pre><code class="language-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">publishConfig</span><span class="hljs-params">(String dataId, String group, String content)</span> <span class="hljs-keyword">throws</span> NacosException</span>;
</code></pre>
<h4>Request parameters</h4>
<table>
<thead>
<tr>
<th style="text-align:left">Name</th>
<th style="text-align:left">Type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">dataId</td>
<td style="text-align:left">string</td>
<td style="text-align:left">Configuration ID. Naming rule is similar to package.class (com.taobao.tc.refund.log.level) is used to ensure the global uniqueness We recommend that you define class by business meaning. All characters must be in lower case. Use alphabetical letters and these four special characters (&quot;.&quot;, &quot;:&quot;, &quot;-&quot;, &quot;_&quot;) only. Up to 256 characters are allowed.</td>
</tr>
<tr>
<td style="text-align:left">group</td>
<td style="text-align:left">string</td>
<td style="text-align:left">Configuration group. We recommend that you use product name: module name (for example Nacos:Test) to ensure the uniqueness. Use alphabetical letters and these four special characters (&quot;.&quot;, &quot;:&quot;, &quot;-&quot;, &quot;_&quot;) only. Up to 128 characters are allowed.</td>
</tr>
<tr>
<td style="text-align:left">content</td>
<td style="text-align:left">string</td>
<td style="text-align:left">Configuration content. No more than 100K bytes.</td>
</tr>
</tbody>
</table>
<h4>Response parameters</h4>
<table>
<thead>
<tr>
<th style="text-align:left">Type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">boolean</td>
<td style="text-align:left">If the publishing is successful</td>
</tr>
</tbody>
</table>
<h4>Request example</h4>
<pre><code class="language-java"><span class="hljs-keyword">try</span> {
    <span class="hljs-comment">// Initialize the configuration service. Retrieves the following parameters in console with sample code</span>
	String serverAddr = <span class="hljs-string">"{serverAddr}"</span>;
	String dataId = <span class="hljs-string">"{dataId}"</span>;
	String group = <span class="hljs-string">"{group}"</span>;
	Properties properties = <span class="hljs-keyword">new</span> Properties();
	properties.put(<span class="hljs-string">"serverAddr"</span>, serverAddr);
    ConfigService configService = NacosFactory.createConfigService(properties);
	<span class="hljs-keyword">boolean</span> isPublishOk = configService.publishConfig(dataId, group, <span class="hljs-string">"content"</span>);
	System.out.println(isPublishOk);
} <span class="hljs-keyword">catch</span> (NacosException e) {
    <span class="hljs-comment">// TODO Auto-generated catch block</span>
    e.printStackTrace();
}
</code></pre>
<h4>Exceptions</h4>
<p>In case of reading configuration timeout or network issues, ConfigException exception is thrown.</p>
<h3>Delete configuration</h3>
<h4>Description</h4>
<p>It deletes Nacos configurations automatically with program to reduce operation and maintenance costs with automation.</p>
<p><strong>Note:</strong> If the specified configuration exists, then it deletes the configuration. If the specified configuration doesn’t exist, then it returns a successful message.</p>
<pre><code class="language-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">removeConfig</span><span class="hljs-params">(String dataId, String group)</span> <span class="hljs-keyword">throws</span> NacosException

</span></code></pre>
<h4>Request parameters</h4>
<table>
<thead>
<tr>
<th style="text-align:left">Parameter name</th>
<th style="text-align:left">Parameter type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">dataId</td>
<td style="text-align:left">String</td>
<td style="text-align:left">Configuration ID</td>
</tr>
<tr>
<td style="text-align:left">group</td>
<td style="text-align:left">String</td>
<td style="text-align:left">Configuration group</td>
</tr>
</tbody>
</table>
<h4>Response parameters</h4>
<table>
<thead>
<tr>
<th style="text-align:left">Parameter type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">boolean</td>
<td style="text-align:left">If the deletion is successful</td>
</tr>
</tbody>
</table>
<h4>Request example</h4>
<pre><code class="language-java"><span class="hljs-keyword">try</span> {
    <span class="hljs-comment">// Initialize the configuration service. Retrieves the following parameters in console with sample code</span>
	String serverAddr = <span class="hljs-string">"{serverAddr}"</span>;
	String dataId = <span class="hljs-string">"{dataId}"</span>;
	String group = <span class="hljs-string">"{group}"</span>;
	Properties properties = <span class="hljs-keyword">new</span> Properties();
	properties.put(<span class="hljs-string">"serverAddr"</span>, serverAddr);

	ConfigService configService = NacosFactory.createConfigService(properties);
	<span class="hljs-keyword">boolean</span> isRemoveOk = configService.removeConfig(dataId, group);
	System.out.println(isRemoveOk);
} <span class="hljs-keyword">catch</span> (NacosException e) {
    <span class="hljs-comment">// TODO Auto-generated catch block</span>
    e.printStackTrace();
}
</code></pre>
<h4>Exceptions</h4>
<p>In case of reading configuration timeout or network issues, ConfigException exception is thrown.</p>
<h2>Service Discovery SDK</h2>
<h3>Register Instance</h3>
<h4>Description</h4>
<p>Register an instance to service.</p>
<pre><code class="language-java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">registerInstance</span><span class="hljs-params">(String serviceName, String ip, <span class="hljs-keyword">int</span> port)</span> <span class="hljs-keyword">throws</span> NacosException</span>;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">registerInstance</span><span class="hljs-params">(String serviceName, String ip, <span class="hljs-keyword">int</span> port, String clusterName)</span> <span class="hljs-keyword">throws</span> NacosException</span>;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">registerInstance</span><span class="hljs-params">(String serviceName, Instance instance)</span> <span class="hljs-keyword">throws</span> NacosException</span>;
</code></pre>
<h4>Request Parameters</h4>
<table>
<thead>
<tr>
<th style="text-align:left">Name</th>
<th style="text-align:left">Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">serviceName</td>
<td style="text-align:left">String</td>
<td>service name</td>
</tr>
<tr>
<td style="text-align:left">ip</td>
<td style="text-align:left">String</td>
<td>instance ip</td>
</tr>
<tr>
<td style="text-align:left">port</td>
<td style="text-align:left">int</td>
<td>instance port</td>
</tr>
<tr>
<td style="text-align:left">clusterName</td>
<td style="text-align:left">String</td>
<td>cluster name</td>
</tr>
<tr>
<td style="text-align:left">instance</td>
<td style="text-align:left">Refer to Java docs</td>
<td>instance properties</td>
</tr>
</tbody>
</table>
<h4>Response</h4>
<p>void</p>
<h4>Request Example</h4>
<pre><code class="language-java">NamingService naming = NamingFactory.createNamingService(System.getProperty(<span class="hljs-string">"serveAddr"</span>));
naming.registerInstance(<span class="hljs-string">"nacos.test.3"</span>, <span class="hljs-string">"11.11.11.11"</span>, <span class="hljs-number">8888</span>, <span class="hljs-string">"TEST1"</span>);

Instance instance = <span class="hljs-keyword">new</span> Instance();
instance.setIp(<span class="hljs-string">"55.55.55.55"</span>);
instance.setPort(<span class="hljs-number">9999</span>);
instance.setHealthy(<span class="hljs-keyword">false</span>);
instance.setWeight(<span class="hljs-number">2.0</span>);
Map&lt;String, String&gt; instanceMeta = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();
instanceMeta.put(<span class="hljs-string">"site"</span>, <span class="hljs-string">"et2"</span>);
instance.setMetadata(instanceMeta);

Service service = <span class="hljs-keyword">new</span> Service(<span class="hljs-string">"nacos.test.4"</span>);
service.setApp(<span class="hljs-string">"nacos-naming"</span>);
service.setHealthCheckMode(<span class="hljs-string">"server"</span>);
service.setProtectThreshold(<span class="hljs-number">0.8F</span>);
service.setGroup(<span class="hljs-string">"CNCF"</span>);
Map&lt;String, String&gt; serviceMeta = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();
serviceMeta.put(<span class="hljs-string">"symmetricCall"</span>, <span class="hljs-string">"true"</span>);
service.setMetadata(serviceMeta);
instance.setService(service);

Cluster cluster = <span class="hljs-keyword">new</span> Cluster();
cluster.setName(<span class="hljs-string">"TEST5"</span>);
AbstractHealthChecker.Http healthChecker = <span class="hljs-keyword">new</span> AbstractHealthChecker.Http();
healthChecker.setExpectedResponseCode(<span class="hljs-number">400</span>);
healthChecker.setCurlHost(<span class="hljs-string">"USer-Agent|Nacos"</span>);
healthChecker.setCurlPath(<span class="hljs-string">"/xxx.html"</span>);
cluster.setHealthChecker(healthChecker);
Map&lt;String, String&gt; clusterMeta = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();
clusterMeta.put(<span class="hljs-string">"xxx"</span>, <span class="hljs-string">"yyyy"</span>);
cluster.setMetadata(clusterMeta);

instance.setCluster(cluster);

naming.registerInstance(<span class="hljs-string">"nacos.test.4"</span>, instance);
</code></pre>
<h3>Deregister Instance</h3>
<h4>Description</h4>
<p>Remove instance from service.</p>
<pre><code class="language-java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">deregisterInstance</span><span class="hljs-params">(String serviceName, String ip, <span class="hljs-keyword">int</span> port)</span> <span class="hljs-keyword">throws</span> NacosException</span>;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">deregisterInstance</span><span class="hljs-params">(String serviceName, String ip, <span class="hljs-keyword">int</span> port, String clusterName)</span> <span class="hljs-keyword">throws</span> NacosException</span>;
</code></pre>
<h4>Request Parameters</h4>
<table>
<thead>
<tr>
<th style="text-align:left">Name</th>
<th style="text-align:left">Type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">serviceName</td>
<td style="text-align:left">String</td>
<td style="text-align:left">service name</td>
</tr>
<tr>
<td style="text-align:left">ip</td>
<td style="text-align:left">String</td>
<td style="text-align:left">instance ip</td>
</tr>
<tr>
<td style="text-align:left">port</td>
<td style="text-align:left">int</td>
<td style="text-align:left">instance port</td>
</tr>
<tr>
<td style="text-align:left">clusterName</td>
<td style="text-align:left">String</td>
<td style="text-align:left">cluster name</td>
</tr>
</tbody>
</table>
<h4>Response</h4>
<p>None</p>
<h4>Request Example</h4>
<pre><code class="language-java">NamingService naming = NamingFactory.createNamingService(System.getProperty(<span class="hljs-string">"serveAddr"</span>));
naming.deregisterInstance(<span class="hljs-string">"nacos.test.3"</span>, <span class="hljs-string">"11.11.11.11"</span>, <span class="hljs-number">8888</span>, <span class="hljs-string">"DEFAULT"</span>);
</code></pre>
<h3>Get all instances of service</h3>
<h4>Description</h4>
<p>Get all instances of service.</p>
<pre><code class="language-java"><span class="hljs-function">List&lt;Instance&gt; <span class="hljs-title">getAllInstances</span><span class="hljs-params">(String serviceName)</span> <span class="hljs-keyword">throws</span> NacosException</span>;

<span class="hljs-function">List&lt;Instance&gt; <span class="hljs-title">getAllInstances</span><span class="hljs-params">(String serviceName, List&lt;String&gt; clusters)</span> <span class="hljs-keyword">throws</span> NacosException</span>;
</code></pre>
<h4>Request Parameters</h4>
<table>
<thead>
<tr>
<th style="text-align:left">Name</th>
<th style="text-align:left">Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">serviceName</td>
<td style="text-align:left">String</td>
<td>service name</td>
</tr>
<tr>
<td style="text-align:left">clusters</td>
<td style="text-align:left">List</td>
<td>cluster list</td>
</tr>
</tbody>
</table>
<h4>Response</h4>
<p>List<Instance> instance list。</p>
<h4>Request Example</h4>
<pre><code class="language-java">NamingService naming = NamingFactory.createNamingService(System.getProperty(<span class="hljs-string">"serveAddr"</span>));
System.out.println(naming.getAllInstances(<span class="hljs-string">"nacos.test.3"</span>));
</code></pre>
<h3>Get filtered list of instance</h3>
<h4>Description</h4>
<p>Get healthy or unhealthy instances of service.</p>
<pre><code class="language-java"><span class="hljs-function">List&lt;Instance&gt; <span class="hljs-title">selectInstances</span><span class="hljs-params">(String serviceName, <span class="hljs-keyword">boolean</span> healthy)</span> <span class="hljs-keyword">throws</span> NacosException</span>;

<span class="hljs-function">List&lt;Instance&gt; <span class="hljs-title">selectInstances</span><span class="hljs-params">(String serviceName, List&lt;String&gt; clusters, <span class="hljs-keyword">boolean</span> healthy)</span> <span class="hljs-keyword">throws</span> NacosException</span>;
</code></pre>
<h4>Request Parameters</h4>
<table>
<thead>
<tr>
<th style="text-align:left">Name</th>
<th style="text-align:left">Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">serviceName</td>
<td style="text-align:left">String</td>
<td>service name</td>
</tr>
<tr>
<td style="text-align:left">clusters</td>
<td style="text-align:left">List</td>
<td>cluster list</td>
</tr>
<tr>
<td style="text-align:left">healthy</td>
<td style="text-align:left">boolean</td>
<td>healthy or not</td>
</tr>
</tbody>
</table>
<h4>Response</h4>
<p>List<Instance> instance list.</p>
<h4>Request Example</h4>
<pre><code class="language-java">NamingService naming = NamingFactory.createNamingService(System.getProperty(<span class="hljs-string">"serveAddr"</span>));
System.out.println(naming.selectInstances(<span class="hljs-string">"nacos.test.3"</span>, <span class="hljs-keyword">true</span>));
</code></pre>
<h3>Get one healthy instance</h3>
<h4>Description</h4>
<p>Get one healthy instance selected by load-balance strategy.</p>
<pre><code class="language-java"><span class="hljs-function">Instance <span class="hljs-title">selectOneHealthyInstance</span><span class="hljs-params">(String serviceName)</span> <span class="hljs-keyword">throws</span> NacosException</span>;

<span class="hljs-function">Instance <span class="hljs-title">selectOneHealthyInstance</span><span class="hljs-params">(String serviceName, List&lt;String&gt; clusters)</span> <span class="hljs-keyword">throws</span> NacosException</span>;
</code></pre>
<h4>Request Parameters</h4>
<table>
<thead>
<tr>
<th style="text-align:left">Name</th>
<th style="text-align:left">Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">serviceName</td>
<td style="text-align:left">String</td>
<td>service name</td>
</tr>
<tr>
<td style="text-align:left">clusters</td>
<td style="text-align:left">List</td>
<td>cluster list</td>
</tr>
</tbody>
</table>
<h4>Response</h4>
<p>Instance</p>
<h4>Request Example</h4>
<pre><code class="language-java">NamingService naming = NamingFactory.createNamingService(System.getProperty(<span class="hljs-string">"serveAddr"</span>));
System.out.println(naming.selectOneHealthyInstance(<span class="hljs-string">"nacos.test.3"</span>));
</code></pre>
<h3>Listen Service</h3>
<h4>Description</h4>
<p>Listen for changes of instances under a service.</p>
<pre><code class="language-java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">subscribe</span><span class="hljs-params">(String serviceName, EventListener listener)</span> <span class="hljs-keyword">throws</span> NacosException</span>;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">subscribe</span><span class="hljs-params">(String serviceName, List&lt;String&gt; clusters, EventListener listener)</span> <span class="hljs-keyword">throws</span> NacosException</span>;
</code></pre>
<h4>Request Parameters</h4>
<table>
<thead>
<tr>
<th style="text-align:left">Name</th>
<th style="text-align:left">Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">serviceName</td>
<td style="text-align:left">String</td>
<td>service name</td>
</tr>
<tr>
<td style="text-align:left">clusters</td>
<td style="text-align:left">List</td>
<td>cluster list</td>
</tr>
<tr>
<td style="text-align:left">listener</td>
<td style="text-align:left">EventListener</td>
<td>event listener</td>
</tr>
</tbody>
</table>
<h4>Response</h4>
<p>void</p>
<h4>Request Example</h4>
<pre><code class="language-java">NamingService naming = NamingFactory.createNamingService(System.getProperty(<span class="hljs-string">"serveAddr"</span>));
naming.subscribe(<span class="hljs-string">"nacos.test.3"</span>, event -&gt; {
    <span class="hljs-keyword">if</span> (event <span class="hljs-keyword">instanceof</span> NamingEvent) {
        System.out.println(((NamingEvent) event).getServceName());
        System.out.println(((NamingEvent) event).getInstances());
    }
});
</code></pre>
<h3>Unlisten Service</h3>
<h4>Description</h4>
<p>Cancel listening service.</p>
<pre><code class="language-java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">unsubscribe</span><span class="hljs-params">(String serviceName, EventListener listener)</span> <span class="hljs-keyword">throws</span> NacosException</span>;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">unsubscribe</span><span class="hljs-params">(String serviceName, List&lt;String&gt; clusters, EventListener listener)</span> <span class="hljs-keyword">throws</span> NacosException</span>;
</code></pre>
<h4>Request Parameters</h4>
<table>
<thead>
<tr>
<th style="text-align:left">Name</th>
<th style="text-align:left">Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">serviceName</td>
<td style="text-align:left">String</td>
<td>service name</td>
</tr>
<tr>
<td style="text-align:left">clusters</td>
<td style="text-align:left">List</td>
<td>cluster list</td>
</tr>
<tr>
<td style="text-align:left">listener</td>
<td style="text-align:left">EventListener</td>
<td>event listener</td>
</tr>
</tbody>
</table>
<h4>Response</h4>
<p>void</p>
<h4>Request Example</h4>
<pre><code class="language-java">NamingService naming = NamingFactory.createNamingService(System.getProperty(<span class="hljs-string">"serveAddr"</span>));
naming.unsubscribe(<span class="hljs-string">"nacos.test.3"</span>, event -&gt; {});
</code></pre>
<h3>Use Authentication Configuration</h3>
<h4>Description</h4>
<p>By default, no login is required to start following the official document configuration, which can expose the configuration center directly to the outside world.</p>
<pre><code class="language-java">### If turn on auth system:
nacos.core.auth.enabled=false
</code></pre>
<p>Therefore, to enable authentication, use nacos by configuring the user name and password.</p>
<pre><code class="language-java">### If turn on auth system:
nacos.core.auth.enabled=true
</code></pre>
<h4>Example Code</h4>
<pre><code class="language-java"><span class="hljs-keyword">try</span> {
    <span class="hljs-comment">// Initialize the configuration service, and the console automatically obtains the following parameters through the sample code.</span>
	String serverAddr = <span class="hljs-string">"{serverAddr}"</span>;
	Properties properties = <span class="hljs-keyword">new</span> Properties();
	properties.put(<span class="hljs-string">"serverAddr"</span>, serverAddr);

    <span class="hljs-comment">// if need username and password to login</span>
        properties.put(<span class="hljs-string">"username"</span>,<span class="hljs-string">"nacos"</span>);
        properties.put(<span class="hljs-string">"password"</span>,<span class="hljs-string">"nacos"</span>);

	ConfigService configService = NacosFactory.createConfigService(properties);
} <span class="hljs-keyword">catch</span> (NacosException e) {
    <span class="hljs-comment">// TODO Auto-generated catch block</span>
    e.printStackTrace();
}
</code></pre>
</div></div></section><footer class="footer-container"><div class="footer-body"><img src="/img/nacos_gray.png"/><div class="cols-container"><div class="col col-12"><h3>Vision</h3><p>By providing an easy-to-use service infrastructure such as dynamic service discovery, service configuration, service sharing and management and etc., Nacos help users better construct, deliver and manage their own service platform, reuse and composite business service faster and deliver value of business innovation more quickly so as to win market for users in the era of cloud native and in all cloud environments, such as private, mixed, or public clouds.</p></div><div class="col col-6"><dl><dt>Documentation</dt><dd><a href="/en-us/docs/what-is-nacos.html" target="_self">Overview</a></dd><dd><a href="/en-us/docs/quick-start.html" target="_self">Quick start</a></dd><dd><a href="/en-us/docs/contributing.html" target="_self">Developer guide</a></dd></dl></div><div class="col col-6"><dl><dt>Resources</dt><dd><a href="/en-us/community/index.html" target="_self">Community</a></dd><dd><a href="https://www.aliyun.com/product/acm?source_type=nacos_pc_20181219" target="_self">Cloud Service ACM</a></dd><dd><a href="https://www.aliyun.com/product/edas?source_type=nacos_pc_20181219" target="_self">Cloud Service EDAS</a></dd><dd><a href="https://www.aliyun.com/product/ahas?source_type=nacos_pc_20190225" target="_self">Cloud Service AHAS</a></dd></dl></div></div><div class="copyright"><span>@ 2018 The Nacos Authors | An Alibaba Middleware (Aliware) Project</span></div></div></footer></div></div>
	<script src="https://f.alicdn.com/react/15.4.1/react-with-addons.min.js"></script>
	<script src="https://f.alicdn.com/react/15.4.1/react-dom.min.js"></script>
	<script>
		window.rootPath = '';
  </script>
	<script src="/build/documentation.js"></script>
</body>
</html>
